package com.ruoyi.project.wenyanwen.article.controller;

import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.wenyanwen.article.domain.Article;
import com.ruoyi.project.wenyanwen.article.service.IArticleService;
import com.ruoyi.project.wenyanwen.tag.domain.ArticleTag;
import com.ruoyi.project.wenyanwen.tag.service.IArticleTagService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 内容管理Controller
 *
 * @author shixiangwen
 * @date 2020-08-05
 */
@Controller
@RequestMapping("/wenyanwen/article")
public class ArticleController extends BaseController {
    private String prefix = "wenyanwen/article";

    @Autowired
    private IArticleService articleService;

    @Autowired
    private IArticleTagService articleTagService;

    @RequiresPermissions("wenyanwen:article:view")
    @GetMapping()
    public String article() {
        return prefix + "/article";
    }

    /**
     * 查询内容管理列表
     */
    @RequiresPermissions("wenyanwen:article:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Article article) {
        startPage();
        List<Article> list = articleService.selectArticleList(article);
        return getDataTable(list);
    }

    /**
     * 导出内容管理列表
     */
    @RequiresPermissions("wenyanwen:article:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Article article) {
        List<Article> list = articleService.selectArticleList(article);
        ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
        return util.exportExcel(list, "article");
    }

    /**
     * 新增内容管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {

        List<ArticleTag> tags = articleTagService.selectArticleTagList(new ArticleTag());
        mmap.put("tags", tags);

        return prefix + "/add";
    }

    /**
     * 新增保存内容管理
     */
    @RequiresPermissions("wenyanwen:article:add")
    @Log(title = "内容管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Article article) {
        return toAjax(articleService.insertArticle(article));
    }

    /**
     * 修改内容管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {

        Article article = articleService.selectArticleById(id);
        mmap.put("article", article);

        List<Long> articleTags = null;
        if (!StringUtils.isEmpty(article.getTag())) {
            articleTags = JsonUtil.toListObject(article.getTag(), Long.class);
        } else {
            articleTags = new ArrayList<>();
        }

        List<ArticleTag> tags = articleTagService.selectArticleTagList(new ArticleTag());
        for (ArticleTag tag : tags) {
            if (articleTags.contains(tag.getTagId())) {
                tag.setFlag(true);
            }
        }
        mmap.put("tags", tags);

        return prefix + "/edit";
    }

    /**
     * 修改对照翻译
     */
    @GetMapping("/yiwenEdit/{id}")
    public String yiwenEdit(@PathVariable("id") Long id, ModelMap mmap) {

        Article article = articleService.selectArticleById(id);
        mmap.put("article", article);

        return prefix + "/yiwenEdit";
    }

    /**
     * 修改对照注解
     */
    @GetMapping("/zhujieEdit/{id}")
    public String zhujieEdit(@PathVariable("id") Long id, ModelMap mmap) {

        Article article = articleService.selectArticleById(id);
        mmap.put("article", article);

        return prefix + "/zhujieEdit";
    }


    /**
     * 修改保存内容管理
     */
    @RequiresPermissions("wenyanwen:article:edit")
    @Log(title = "内容管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Article article) {
        return toAjax(articleService.updateArticle(article));
    }

    /**
     * 删除内容管理
     */
    @RequiresPermissions("wenyanwen:article:remove")
    @Log(title = "内容管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(articleService.deleteArticleByIds(ids));
    }
}
